package com.bh.statistics.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bh.statistics.dao.TBcastMapper;
import com.bh.statistics.entity.ResultPage;
import com.bh.statistics.entity.TBcast;
import com.bh.statistics.entity.TBcastVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * BroadcastServiceImpl
 *
 * @author Ljy
 * @version 2023/09/05 17:07
 **/
@Service
@Slf4j
public class BroadcastServiceImpl implements BroadcastService {
    @Resource
    TBcastMapper tBcastMapper;

    @Override
    public ResultPage<TBcast> getPage(Integer page, Integer limit, TBcast broadcast) {
        Page<TBcast> queryPage = new Page<>();
        QueryWrapper<TBcast> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(broadcast.getEbmId())) {
            queryWrapper.like("ebm_id", broadcast.getEbmId());
        }
        if (broadcast.getEventLevel() != null) {
            queryWrapper.eq("event_level", broadcast.getEventLevel());
        }
        if (broadcast.getStatus() != null) {
            queryWrapper.eq("status", broadcast.getStatus());
        }
        if (StrUtil.isNotEmpty(broadcast.getTargetCodes())) {
            String targetCodes = broadcast.getTargetCodes();
            String[] split = targetCodes.split(",");
            for (String s : split) {
                String newStr = formatterCode(s);
                if (StrUtil.isNotEmpty(newStr)) {
                    queryWrapper.like("target_codes", "%" + newStr + "%");
                }
            }
        }
        if (StrUtil.isNotEmpty(broadcast.getStartDate1())) {
            String startDate1 = broadcast.getStartDate1();
            String[] split = startDate1.split("~");
            queryWrapper.between("start_time_fact", split[0], split[1]);
        }
        List<TBcast> resultList;
        int total;
        if (page != null && limit != null) {
            queryPage.setCurrent(page);
            queryPage.setSize(limit);
            IPage<TBcast> resultPage = tBcastMapper.selectPage(queryPage, queryWrapper);
            resultList = resultPage.getRecords();
            total = (int) resultPage.getTotal();
        } else {
            resultList = tBcastMapper.selectList(queryWrapper);
            total = resultList.size();
        }
        return new ResultPage<>(0, "", total, resultList);
    }

    private String formatterCode(String code) {
        String sheng = code.substring(0, 2);
        String shi = code.substring(2, 4);
        String xian = code.substring(4, 6);
        String zhen = code.substring(6, 9);
        String cun = code.substring(9);
        String[] split = new String[5];
        split[0] = sheng;
        split[1] = shi;
        split[2] = xian;
        split[3] = zhen;
        split[4] = cun;
        String newStr = split[0] + split[1] + split[2] + split[3] + split[4];
        if ("00".equals(split[0])) {
            newStr = "";
        } else if ("00".equals(split[1])) {
            newStr = split[0];
        } else if ("00".equals(split[2])) {
            newStr = split[0] + split[1];
        } else if ("000".equals(split[3])) {
            newStr = split[0] + split[1] + split[2];
        } else if ("000".equals(split[4])) {
            newStr = split[0] + split[1] + split[2] + split[3];
        }
        return newStr;
    }

    @Override
    public List<TBcastVO> getBroadcastHistory(String sourceCode,String startTime, String endTime) {
        List<TBcastVO> bcastList = tBcastMapper.getBroadcastHistory(sourceCode,startTime,endTime);
        return bcastList;
    }
    @Override
    public List<TBcastVO> getBroadcastAdapter(String sourceCode) {
        List<TBcastVO> bcastList = tBcastMapper.getBroadcastAdapter(sourceCode);
        return bcastList;
    }
}
